import type { OrderInfoTypeResult } from '../types'
import { getOrderInfo, getOrderCardInfo } from '../apis/index'

import { OrderSource } from '../enums/index'

// import { parseQuery } from '../utils/parseQuery'

/**
 * @description 获取订单新增
 */
export const useOrderEffect = () => {
  const route = useRoute()

  // const { orderSns, orderSn } = getQueryParams(payOrderId) 获取加密数据 废弃

  const { orderSns, orderSn } = route.query as Record<string, string | any>

  const isOrderList = !!orderSns
  const orderSnsList = isOrderList ? JSON.parse(decodeURIComponent(orderSns)) : ''

  // 订单编号参数
  const orderParams = {
    orderSource: OrderSource.KukeOnlineOrder,
    orderSn: '',
    orderSns: ''
  }
  isOrderList ? (orderParams.orderSns = orderSnsList) : (orderParams.orderSn = orderSn)

  const orderInfo = ref<OrderInfoTypeResult>()

  // 排除0元订单
  const excludeZeroOrder = ref<string[]>([])
  const getOrderData = async () => {
    let res = null
    if (isOrderList) {
      res = await getOrderCardInfo(orderParams)
      excludeZeroOrder.value = res.data.nonZeroOrderSns as string[]
    } else {
      res = await getOrderInfo(orderParams)
    }

    orderInfo.value = res.data
  }

  onMounted(() => {
    getOrderData()
  })

  return {
    excludeZeroOrder,
    orderInfo
  }
}
